cmake_minimum_required(VERSION 2.8.9)

project(libwebsockets C)


write_file(lib.sh "#!/bin/bash
prj_builddir=`pwd`
git clone https://gitee.com/jinquan711/MySQL-Connector-C-6.1.11
cd MySQL-Connector-C-6.1.11 && mkdir build && mkdir mysqlcc && cd build
cmake -DCMAKE_INSTALL_PREFIX=\"../mysqlcc\" ..
make -j4
make install
cd ../..
make -C ${CMAKE_CURRENT_BINARY_DIR}/libwebsockets-20191216
")
execute_process(COMMAND chmod 755 ${CMAKE_CURRENT_BINARY_DIR}/lib.sh )

#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/LIBS)
add_subdirectory(libwebsockets-20191216)

 
#
# Client / Server / Test Apps build control
#
option(myLWS_WITHOUT_CLIENT "Don't build the client part of the library" OFF)
option(myLWS_WITHOUT_SERVER "Don't build the server part of the library" OFF)
option(myLWS_WITHOUT_TESTAPPS "Don't build the libwebsocket-test-apps" OFF)
option(myLWS_WITHOUT_TEST_SERVER "Don't build the test server" OFF)
option(myLWS_WITHOUT_TEST_SERVER_EXTPOLL "Don't build the test server version that uses external poll" OFF)
option(myLWS_WITHOUT_TEST_PING "Don't build the ping test application" OFF)
option(myLWS_WITHOUT_TEST_CLIENT "Don't build the client test application" OFF)
option(myLWS_ROLE_H1 "Compile with support for http/1 (needed for ws)" ON)
option(myLWS_WITH_HTTP2 "Compile with server support for HTTP/2" ON)
if (myLWS_WITH_HTTP2)
	set(myLWS_ROLE_H2 1)
endif()

#
# Test applications
#
if ((myLWS_ROLE_H1 OR myLWS_ROLE_H2) AND NOT myLWS_WITHOUT_TESTAPPS)
	MESSAGE(STATUS "CMAKE_INSTALL_PREFIX ->> ${CMAKE_INSTALL_PREFIX}")
	if (NOT myLWS_WITHOUT_SERVER)
		#
		# test-server
		#

		# @@jinquan added.
		INCLUDE_DIRECTORIES( 
			#"/usr/include/mysql"
			"${CMAKE_CURRENT_BINARY_DIR}/MySQL-Connector-C-6.1.11/mysqlcc/include"
			"${CMAKE_CURRENT_BINARY_DIR}/libwebsockets-20191216"
			"${CMAKE_CURRENT_BINARY_DIR}/libwebsockets-20191216/include"
            "${CMAKE_CURRENT_SOURCE_DIR}/jinquan"
		)
		
		# @@jinquan added.
		LINK_DIRECTORIES(
			#"/usr/local/lib"  		#libwebsockets.a
			"${CMAKE_CURRENT_BINARY_DIR}/libwebsockets-20191216/lib"
			"/usr/lib64"      		#libssl.so libcrypto.so
			#"/usr/lib64/mysql"  	#mysqlclient.a
			"${CMAKE_CURRENT_BINARY_DIR}/MySQL-Connector-C-6.1.11/mysqlcc/lib"
		)

		if (NOT myLWS_WITHOUT_TEST_SERVER)
			ADD_EXECUTABLE(server 
				"jinquan/cJSON.c"		# @@jinquan added.
				"jinquan/myapp.c"		# @@jinquan added.
				"jinquan/mysqldb.c"		# @@jinquan added.
                "jinquan/dblink_string.c"
				"test-server.c"
				"")
			set_property(
				TARGET server
				PROPERTY COMPILE_DEFINITIONS  
					INSTALL_DATADIR="${CMAKE_INSTALL_PREFIX}/share"
				)
		endif()

		#
		# test-server-extpoll
		#
		if (NOT myLWS_WITHOUT_TEST_SERVER_EXTPOLL AND NOT WIN32)
			ADD_EXECUTABLE(server-epoll
				"jinquan/cJSON.c"		# @@jinquan added.
				"jinquan/myapp.c"		# @@jinquan added.
				"jinquan/mysqldb.c"		# @@jinquan added.
                "jinquan/dblink_string.c"
				"test-server.c"
				"")
			# Set defines for this executable only.
			set_property(
				TARGET server-epoll
				PROPERTY COMPILE_DEFINITIONS 
					EXTERNAL_POLL 
					INSTALL_DATADIR="${CMAKE_INSTALL_PREFIX}/share"
				)
		endif()

	endif(NOT myLWS_WITHOUT_SERVER)

	if (NOT myLWS_WITHOUT_CLIENT)
		#
		# test-client
		#
		if (NOT myLWS_WITHOUT_TEST_CLIENT)
			ADD_EXECUTABLE(client "test-client.c" "" "" "" "" "")
		endif()

	endif(NOT myLWS_WITHOUT_CLIENT)
endif((myLWS_ROLE_H1 OR myLWS_ROLE_H2) AND NOT myLWS_WITHOUT_TESTAPPS)

MESSAGE(STATUS " +=======================================================================+ ")
MESSAGE(STATUS " |  MUST RUN ->> 'source ${CMAKE_CURRENT_BINARY_DIR}/lib.sh' FIRST!          | ")
MESSAGE(STATUS " +=======================================================================+ ")
add_custom_command(TARGET server
                           PRE_BUILD
    #COMMAND make -C ${CMAKE_CURRENT_BINARY_DIR}/libwebsockets-20191216   VERBATIM
    COMMAND echo "pre build...test..."
    #COMMAND eval ${CMAKE_CURRENT_BINARY_DIR}/lib.sh
)

target_link_libraries(server        -lm -ldl -lstdc++ -lpthread libwebsockets.a -lssl -lcrypto libmysqlclient.a)	# @@jinquan added
target_link_libraries(server-epoll  -lm -ldl -lstdc++ -lpthread libwebsockets.a -lssl -lcrypto libmysqlclient.a)	# @@jinquan added
target_link_libraries(client        -lm -ldl -lstdc++ -lpthread libwebsockets.a -lssl -lcrypto libmysqlclient.a)	# @@jinquan added

add_custom_command(TARGET server server-epoll client
                           POST_BUILD
    COMMAND "${CMAKE_COMMAND}" -E make_directory  "$<TARGET_FILE_DIR:server>/lwsapp/resource"   VERBATIM
    COMMAND "${CMAKE_COMMAND}" -E copy_directory  ${CMAKE_CURRENT_SOURCE_DIR}/resource      "$<TARGET_FILE_DIR:server>/lwsapp/resource"     VERBATIM
    COMMAND "${CMAKE_COMMAND}" -E copy  ${CMAKE_CURRENT_SOURCE_DIR}/libwebsockets-test-server.key.pem   "$<TARGET_FILE_DIR:server>/lwsapp/lws.key.pem"  VERBATIM
    COMMAND "${CMAKE_COMMAND}" -E copy  ${CMAKE_CURRENT_SOURCE_DIR}/libwebsockets-test-server.pem       "$<TARGET_FILE_DIR:server>/lwsapp/lws.crt.pem"  VERBATIM
    COMMAND "${CMAKE_COMMAND}" -E copy  ${CMAKE_CURRENT_SOURCE_DIR}/dblink.cfg  "$<TARGET_FILE_DIR:server>/lwsapp/dblink.cfg"   VERBATIM
    COMMAND "${CMAKE_COMMAND}" -E copy  ${CMAKE_CURRENT_BINARY_DIR}/server        "$<TARGET_FILE_DIR:server>/lwsapp"      VERBATIM
    COMMAND "${CMAKE_COMMAND}" -E copy  ${CMAKE_CURRENT_BINARY_DIR}/server-epoll  "$<TARGET_FILE_DIR:server-epoll>/lwsapp"        VERBATIM
    COMMAND "${CMAKE_COMMAND}" -E copy  ${CMAKE_CURRENT_BINARY_DIR}/client        "$<TARGET_FILE_DIR:client>/lwsapp"      VERBATIM
)

